package com.quan.hr.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.vo.R;
import com.quan.commons.core.MyBaseController;
import com.quan.commons.utils.PageUtils;
import com.quan.commons.annotation.OperationLog;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.quan.hr.commons.vo.HrCalendarVo;
import com.quan.hr.entity.HrCalendar;
import com.quan.hr.service.HrCalendarService;


/**
 * 项目管理系统-人事管理-工作日历
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2023-02-16 13:19:42
 */
@Slf4j
@Api(tags = "项目管理系统-人事管理-工作日历")
@RestController
@RequestMapping("/hr/hrcalendar")
public class HrCalendarController extends MyBaseController {

    @Autowired
    private HrCalendarService service;

    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "初始化日历")
    @GetMapping("initCalendar")
    public R initCalendar(@RequestParam(name = "year") Integer year, @RequestParam(name = "month", required = false) Integer month) {

        if(null == month) {
            this.service.initCalendar(year);
        } else {
            this.service.initCalendar(year, month);
        }

        return R.ok();
    }

    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "初始化节假日")
    @GetMapping("initCalendarHoliday")
    public R initCalendarHoliday(@RequestParam(name = "year") Integer year, @RequestParam(name = "month", required = false) Integer month) {

        final Map<String, Map<String, Object>> holiday = this.service.initCalendarHoliday(year, month);

        if(holiday.isEmpty()) {
            return R.failure("国家未发布"+year+"年节假日");
        } else {
            return R.ok();
        }
    }


    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "查询日历某月")
    @GetMapping("list")
    public R list(@RequestParam(name = "year") String year, @RequestParam(name = "month") String month) {

        final List<HrCalendar> list = this.service.getList(year, month);

        return R.ok().data(list);
    }

    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "查询某个范围的日历（不包含周末，节假日）")
    @GetMapping("getBetweenCalendarWorking")
    public R getBetweenCalendarWorking(@RequestParam(name = "start") String start, @RequestParam(name = "end") String end) {

        final List<HrCalendar> list = this.service.getBetweenCalendarWorking(start, end);

        return R.ok().data(list);
    }


    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "获取某年某月的实际工作天数")
    @GetMapping("getWorkingCount")
    public R getWorkingCount(@RequestParam(name = "year") String year, @RequestParam(name = "month") String month) {

        return R.ok().data(this.service.getWorkingCount(Integer.parseInt(year), Integer.parseInt(month)));
    }

    /**
     * 设置节假日
     * @param vo
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "设置节假日")
    @PostMapping("setHoliday")
    public R setHoliday(@Valid @RequestBody HrCalendarVo vo) {
        this.service.setHoliday(vo);
        return R.ok();
    }

    /**
     * 保存
     * @param vo
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "新增")
    @PostMapping("save")
    public R save(@Valid @RequestBody HrCalendarVo vo) {
        this.service.save(vo);
        return R.ok();
    }

    /**
     * 修改
     * @param vo
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "修改")
    @PostMapping("update")
    public R update(@Valid @RequestBody HrCalendarVo vo) {
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "删除")
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     * @param vo
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "查询详情")
    @GetMapping("/info")
    public R info(HrCalendarVo vo) {
        QueryWrapper<HrCalendar> queryWrapper = new QueryWrapper<HrCalendar>(vo);
        HrCalendar hrCalendar =this.service.getOne(queryWrapper);
        return R.ok().data(hrCalendar);
    }

    /**
     * 根据ID查询信息详情
     * @param {calendarDate}
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "查询详情")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("calendarDate") LocalDate calendarDate) {
        HrCalendar hrCalendar =this.service.getById(calendarDate);
        return R.ok().data(hrCalendar);
    }

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @OperationLog(module = "项目管理系统-人事管理-工作日历", title = "查询列表")
    @GetMapping(value = "page")
    public R queryPage(HrCalendarVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().data(page);
    }

}
